package com.zhai.cloud.common.domain;




import com.zhai.cloud.common.constant.GlobalConstant;
import lombok.Data;

import java.io.Serializable;

/**
 * @package: com.zhai.boot.common
 * @project-name: zhai-cloud
 * @description: 统一结果集（所有service层统一返回结果对象）
 * @author: Created by 翟凤玺
 * @create-datetime: 2018-7-10 11:50
 */
@Data
public class Result<T> implements Serializable {

    /**
     * 状态码
     */
    private String code;
    /**
     * 信息
     */
    private String msg;
    /**
     * 结果
     */
    private T result;

    /**
     * 自定义
     */
    public static <T> Result<T> defined(Result<T> result) {
        result.setCode(result.getCode());
        result.setMsg(result.getMsg());
        result.setResult(result.getResult());
        return result;
    }

    /**
     * 返回成功业务
     */
    public static <T> Result<T> success() {
        Result<T> result = new Result<T>();
        result.setCode(GlobalConstant.RESULT_SUCCESS_CODE);
        result.setMsg(GlobalConstant.RESULT_SUCCESS_MSG);
        result.setResult(null);
        return defined(result);
    }

    /**
     * 返回成功业务
     */
    public static <T> Result<T> success(T result) {
        Result<T> resultObj = new Result<T>();
        resultObj.setCode(GlobalConstant.RESULT_SUCCESS_CODE);
        resultObj.setMsg(GlobalConstant.RESULT_SUCCESS_MSG);
        resultObj.setResult(result);
        return defined(resultObj);
    }

    /**
     * 返回失败业务
     */
    public static <T> Result<T> failure() {
        Result<T> result = new Result<T>();
        result.setCode(GlobalConstant.RESULT_FAILURE_CODE);
        result.setMsg(GlobalConstant.RESULT_FAILURE_MSG);
        result.setResult(null);
        return defined(result);
    }

    /**
     * 返回失败业务
     */
    public static <T> Result<T> failure(String code, String msg, T result) {
        Result<T> resultObj = new Result<T>();
        resultObj.setCode(code);
        resultObj.setMsg(msg);
        resultObj.setResult(result);
        return defined(resultObj);
    }

    /**
     * 返回失败业务
     */
    public static <T> Result<T> failure(String code, String msg) {
        Result<T> result = new Result<T>();
        result.setCode(code);
        result.setMsg(msg);
        result.setResult(null);
        return defined(result);
    }

    /**
     * 返回结果是否成功
     */
    public boolean isSuccess() {
        return this.code.equals(GlobalConstant.RESULT_SUCCESS_CODE);
    }

    /**
     * 返回结果是否失败
     */
    public boolean isFailure() {
        return !this.code.equals(GlobalConstant.RESULT_SUCCESS_CODE);
    }

}
